package com.kingtone.rongji.fragment;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.kingtone.rongji.R;
import com.kingtone.rongji.tool.RongJiRestClient;
import com.kingtone.rongji.tool.RongJiUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class QueryBalanceFragment extends BaseFragment {

	private  TextView title = null;
	private  ImageView backBtn = null;
	private  Button submitBtn = null;
	private  Activity mActivity = null;
	private  EditText cardNo = null;
	private  EditText cardPwd = null;
	private  TextView balanceResult = null;
	
	
	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		View localView = paramLayoutInflater.inflate(R.layout.fragment_query_balance, paramViewGroup, false);
		setupView(localView);
		mActivity = getActivity();
		return localView;
	}
	
	private void setupView(View paramView)
	{
		title = (TextView)paramView.findViewById(R.id.title);
		title.setText(getString(R.string.query_balance_title));
		
		backBtn = (ImageView)paramView.findViewById(R.id.back);
		backBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				getActivity().onBackPressed();
			}
		});
		
		cardNo = (EditText)paramView.findViewById(R.id.query_balance_card_num);
		cardPwd = (EditText)paramView.findViewById(R.id.query_balance_card_pwd);
		balanceResult = (TextView)paramView.findViewById(R.id.card_result);
		submitBtn = (Button)paramView.findViewById(R.id.query_balance_submit);
		submitBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) 
			{
				String cardNoStr = cardNo.getText().toString();
				String cardPwdStr = cardPwd.getText().toString();
				if(TextUtils.isEmpty(cardNoStr))
				{
					Toast.makeText(mActivity, mActivity.getString(R.string.card_input_card_no), Toast.LENGTH_SHORT).show();
					return;
				}
				if(TextUtils.isEmpty(cardPwdStr))
				{
					Toast.makeText(mActivity, mActivity.getString(R.string.card_input_card_pwd), Toast.LENGTH_SHORT).show();
					return;
				}
				getCardBalance(cardNoStr,cardPwdStr);
			}
		});
	}
	
	private void getCardBalance(String cardNo, String pwd)
	{
		RongJiRestClient.getCardBalance(cardNo, pwd, new AsyncHttpResponseHandler() {

			@Override
			public void onStart() {
				RongJiUtils.showEmptyProgress(mActivity);
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				RongJiUtils.dismissProgress();
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				RongJiUtils.dismissProgress();
				
				if(arg2 != null && !new String(arg2).equalsIgnoreCase("null"))
				{
					String result = new String(arg2);
					JSONTokener jsonParser = new JSONTokener(result);  
				    try {
						JSONObject obj = (JSONObject) jsonParser.nextValue();
						boolean errorCode = obj.optBoolean("Status");
						if(errorCode)
						{
							String tag = obj.optString("Tag");
							balanceResult.setText(mActivity.getString(R.string.card_balance)  + tag);
							
						}else
						{
							String errorMsg = obj.optString("ErrorMessage");
							if(!TextUtils.isEmpty(errorMsg))
							{
								balanceResult.setText(errorMsg);
							}
						}
				    } catch (JSONException e) {
						e.printStackTrace();
					} 
				}
			}	
		});
	}
}
